let data = {
  bodyTarget: [
    {
      key: 'height',
      name: '身高',
      value: '',
      unit: '厘米'
    },
    {
      key: 'weight',
      name: '体重',
      value: '',
      unit: '公斤'
    },
    {
      key: 'BMI',
      name: 'BMI',
      value: '',
      unit: ''
    },
    {
      key: 'waistline',
      name: '腰围',
      value: '',
      unit: '厘米'
    }
  ],
  ordinaryTarget: [
    {
      key: 'SBP',
      name: '收缩压（高压）',
      value: '',
      unit: 'mmHg'
    },
    {
      key: 'DBP',
      name: '舒张压（低压）',
      value: '',
      unit: 'mmHg'
    },
    {
      key: 'heartRate',
      name: '心率',
      value: '',
      unit: '次/分钟'
    },
    {
      key: 'FBG',
      name: '空腹血糖',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'PBG',
      name: '餐后2小时血糖',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'HBALC',
      name: '糖化血红蛋白',
      value: '',
      unit: '%'
    },
    {
      key: 'TCHO',
      name: '总胆固醇',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'TG',
      name: '甘油三酯',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'HDL',
      name: '高密度脂蛋白胆固醇',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'LDL',
      name: '低密度脂蛋白胆固醇',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'hPylori',
      name: '幽门螺旋杆菌',
      value: '',
      unit: '',
      options: [
        { name: '阳性', id: 10, tips: '' },
        { name: '阴性', id: 20, tips: '' },
        { name: '没测过', id: 30, tips: '' }
      ]
    },
    {
      key: 'uricAcid',
      name: '尿酸',
      value: '',
      unit: 'μmol/L'
    },
    {
      key: 'Hcy',
      name: '血浆同型半胱氨酸',
      value: '',
      unit: 'μmol/L'
    },
    {
      key: 'ALT',
      name: '谷丙转氨酶ALT',
      value: '',
      unit: 'U/L'
    },
    {
      key: 'AST',
      name: '谷草转氨酶AST',
      value: '',
      unit: 'U/L'
    },
    {
      key: 'serumNa',
      name: '血钠',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'serumKalium',
      name: '血钾',
      value: '',
      unit: 'mmol/L'
    },
    {
      key: 'MAU',
      name: '尿微量白蛋白',
      value: '',
      unit: 'mg/L'
    },
    {
      key: 'Scr',
      name: '血肌酐',
      value: '',
      unit: 'mg/dl' // umol/L
    },
    {
      key: 'eGFR',
      name: '肾小球滤过率eGFR',
      value: '',
      unit: 'ml/min/1.73㎡'
    }
  ],
  ordinaryTargetTextarea: [
    {
      key: 'auscultation',
      name: '听诊检查',
      value: '',
      unit: ''
    },
    {
      key: 'fundoscopy',
      name: '眼底检查',
      value: '',
      unit: ''
    },
    {
      key: 'oralExamination',
      name: '口腔检查',
      value: '',
      unit: ''
    },
    {
      key: 'footExamination',
      name: '足部检查',
      value: '',
      unit: ''
    }
  ],
  illnessTarget: [
    {
      key: 'dysphagia',
      name: '吞咽异常',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1 },
        { name: '否', id: 0 }
      ]
    },
    {
      key: 'stoolAbnormity',
      name: '大便异常',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1 },
        { name: '否', id: 0 }
      ]
    },
    {
      key: 'rightUpperQuadrantPain',
      name: '肝区反射异常',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1 },
        { name: '否', id: 0 }
      ]
    },
    {
      key: 'breastLump',
      name: '乳腺异常',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1 },
        { name: '否', id: 0 }
      ]
    }
  ],
  activeTarget: [
    {
      key: 'drink',
      name: '饮酒情况',
      value: '',
      unit: '',
      options: [
        { name: '几乎从不', id: 10, tips: '' },
        { name: '偶尔', id: 20, tips: '' },
        {
          name: '经常且少量',
          id: 30,
          tips: '每日饮酒量：啤酒不超过250ml,或葡萄酒不超过100ml，或白酒不超过一两'
        },
        {
          name: '经常且中量',
          id: 40,
          tips: '每日饮酒量：啤酒250-500ml，或葡萄酒100-200ml，或白酒一两到二两'
        },
        {
          name: '长期嗜酒',
          id: 50,
          tips: '每日饮酒量：啤酒大于500ml，或葡萄酒大于200ml，或白酒大于二两'
        }
      ]
    },
    {
      key: 'smok',
      name: '吸烟情况',
      value: '',
      unit: '',
      options: [
        { name: '从未', id: 10, tips: '' },
        { name: '不吸烟但经常吸二手烟', id: 20, tips: '' },
        { name: '已戒烟', id: 30 },
        { name: '吸烟', id: 40 }
      ]
    },
    {
      key: 'quitSmokingYear',
      name: '戒烟时长',
      value: '',
      unit: '年'
    },
    {
      key: 'preSmokingYear',
      name: '戒烟前吸烟时长',
      value: '',
      unit: '年'
    },
    {
      key: 'preSmokingBags',
      name: '戒烟前每日吸烟量',
      value: '',
      unit: '包'
    },
    {
      key: 'smokingYear',
      name: '抽烟时长',
      value: '',
      unit: '年'
    },
    {
      key: 'smokingBags',
      name: '每日吸烟量',
      value: '',
      unit: '包'
    },
    {
      key: 'vegetableFruit',
      name: '食用新鲜果蔬',
      value: '',
      unit: '',
      options: [
        { name: '几乎从不', id: 10 },
        { name: '偶尔', id: 20 },
        { name: '经常', id: 30 }
      ]
    },
    {
      key: 'dietaryHabit',
      name: '饮食口味',
      value: '',
      unit: '',
      options: [
        { name: '偏咸', id: 1 },
        { name: '偏甜', id: 2 },
        { name: '清淡', id: 8 },
        { name: '偏油', id: 4 }
      ]
    },
    {
      key: 'stapleFood',
      name: '主食粗细搭配',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1 },
        { name: '否', id: 0 }
      ]
    },
    {
      key: 'meatVegetable',
      name: '饮食荤素搭配',
      value: '',
      unit: '',
      options: [
        { name: '偏荤', id: 10 },
        { name: '偏素', id: 20 },
        { name: '荤素搭配', id: 30 }
      ]
    },
    {
      key: 'eatMeat',
      name: '肉食食用习惯',
      value: '',
      unit: '',
      options: [
        { name: '以猪牛羊肉等红肉为主', id: 10, tips: '' },
        { name: '以鸡鸭鱼肉等白肉为主', id: 20, tips: '' },
        { name: '红白搭配', id: 30, tips: '' },
        { name: '不吃肉', id: 40, tips: '' }
      ]
    },
    {
      key: 'drinking',
      name: '饮水情况',
      value: '',
      unit: '',
      options: [
        { name: '口渴了才会喝水', id: 10, tips: '' },
        { name: '每天主动喝水1-2杯', id: 20, tips: '按每杯容量300ml折算' },
        { name: '每天主动喝水3-4杯', id: 30, tips: '按每杯容量300ml折算' },
        { name: '每天主动喝水5杯及以上', id: 40, tips: '按每杯容量300ml折算' }
      ]
    },
    {
      key: 'isExercise',
      name: '运动情况',
      value: '',
      unit: '',
      options: [
        {
          name: '几乎从不',
          id: 10,
          tips: '一周内基本没有一天达到或超过20分钟的运动'
        },
        {
          name: '偶尔',
          id: 20,
          tips: '每周1-2次达到和超过20分钟以上的专门运动'
        },
        { name: '经常', id: 30, tips: '每周3次以上达到和超过20分钟的专门运动' },
        { name: '总是', id: 40, tips: '几乎每天都达到和超过20分钟的专门运动' }
      ]
    },
    {
      key: 'exerciseMode',
      name: '运动方式',
      value: '',
      unit: '',
      options: [
        { name: '步行', id: 1, tips: '' },
        { name: '快走', id: 2, tips: '' },
        { name: '骑车', id: 4 },
        { name: '游泳', id: 8 },
        { name: '跑步', id: 16 },
        { name: '瑜伽', id: 32 },
        { name: '登山', id: 64 },
        { name: '球类运动', id: 128 },
        { name: '核心/力量训练', id: 256 },
        { name: '其他', id: 512 }
      ]
    },
    {
      key: 'sleepTime',
      name: '睡眠时长',
      value: '',
      unit: '',
      options: [
        { name: '不足6个小时', id: 10, tips: '' },
        { name: '6-7个小时', id: 20, tips: '' },
        { name: '7-8个小时', id: 30, tips: '' },
        { name: '8个小时以上', id: 40, tips: '' }
      ]
    },
    {
      key: 'sleep',
      name: '睡眠质量',
      value: '',
      unit: '',
      options: [
        { name: '一觉到天亮', id: 1, tips: '' },
        { name: '早醒后难入睡', id: 2, tips: '' },
        { name: '入睡困难', id: 3, tips: '' },
        { name: '多梦，常醒', id: 4, tips: '' }
      ]
    },
    {
      key: 'snore',
      name: '是否打鼾',
      value: '',
      unit: '',
      options: [
        { name: '打鼾', id: 1, tips: '' },
        { name: '打鼾伴呼吸暂停', id: 2, tips: '' },
        { name: '无', id: 3, tips: '' }
      ]
    },
    {
      key: 'constipation',
      name: '是否便秘',
      value: '',
      unit: '',
      options: [
        { name: '是', id: 1, tips: '' },
        { name: '否', id: 2, tips: '' }
      ]
    }
  ],
  psychologyTarget: [
    {
      key: 'stress',
      name: '心理状态',
      value: '',
      unit: '',
      options: [
        { name: '没有压力', id: 10, tips: '' },
        { name: '一般压力', id: 20, tips: '' },
        { name: '很大压力', id: 30, tips: '' },
        { name: '几乎崩溃', id: 40, tips: '' }
      ]
    }
  ]
};

// 运动方式和饮食习惯格式化
function reduceTotalExerciseMode(val, options = []) {
  let numItem = [];
  // val = 513;
  if (val && val > 0) {
    if ((val & 1) > 0) {
      numItem.push(1);
    }
    if ((val & 2) > 0) {
      numItem.push(2);
    }
    if ((val & 4) > 0) {
      numItem.push(4);
    }
    if ((val & 8) > 0) {
      numItem.push(8);
    }
    if ((val & 16) > 0) {
      numItem.push(16);
    }
    if ((val & 32) > 0) {
      numItem.push(32);
    }
    if ((val & 64) > 0) {
      numItem.push(64);
    }
    if ((val & 128) > 0) {
      numItem.push(128);
    }
    if ((val & 256) > 0) {
      numItem.push(256);
    }
    if ((val & 512) > 0) {
      numItem.push(512);
    }
  } else {
    numItem = [];
  }
  const filterData = options.filter((item) => numItem.some((id) => id === item.id));
  let newData = [];
  filterData.forEach((item) => {
    newData.push(item.name);
  });
  return newData.join('、');
}

const initData = {
  ALT: null,
  BMI: null,
  DBP: null,
  FBG: null,
  HBALC: null,
  HDL: null,
  Hcy: null,
  LDL: null,
  MAU: null,
  PBG: null,
  PNP: null,
  SBP: null,
  Scr: null,
  TCHO: null,
  TG: null,
  appetite: null,
  auscultation: '',
  bloodUricAcid: null,
  breastLump: null,
  constipation: null,
  createBy: '',
  createDate: '',
  dailyWork: null,
  delFlag: '',
  dietaryHabit: '',
  dietaryHabitOther: null,
  drink: '',
  drinking: '',
  dysphagia: null,
  eGFR: null,
  eatMeat: '',
  examinationDate: null,
  exerciseMode: '',
  exerciseModeOther: '',
  footExamination: '',
  fundoscopy: '',
  hPylori: '',
  heartRate: null,
  height: '',
  id: '',
  isExercise: '',
  meatVegetable: '',
  nutrition: null,
  oralExamination: '',
  patientId: '',
  physicalExaminationDate: '',
  physicalExaminationPics: null,
  picUrls: [],
  preSmokingBags: null,
  preSmokingYear: null,
  quitSmokingYear: null,
  remarks: null,
  rightUpperQuadrantPain: null,
  scrUnit: null,
  serumKalium: null,
  serumNa: null,
  sleep: null,
  sleepTime: null,
  smok: '',
  smokingBags: null,
  smokingYear: null,
  snore: null,
  source: null,
  stapleFood: '',
  stoolAbnormity: null,
  stress: null,
  updateBy: '',
  updateDate: '',
  uricAcid: null,
  vegetableFruit: null,
  waistline: null,
  weight: null
};
// 1、先给上面data所有字段遍历赋值；
export const setDataVal = function (ajaxData = initData) {
  Object.keys(data).forEach((datas) => {
    data[datas].map((item) => {
      // 带选项的赋值
      if (item.options) {
        const optionData = item.options.find((optionItem) => optionItem.id === ajaxData[item.key]);
        item.value = optionData ? optionData.name : '';
        if (item.key === 'exerciseMode') {
          item.value = reduceTotalExerciseMode(ajaxData[item.key], item.options);
          item.otherPlay = ajaxData.exerciseModeOther;
        }
        if (item.key === 'dietaryHabit') {
          item.value = reduceTotalExerciseMode(ajaxData[item.key], item.options);
        }
      } else {
        // 文本赋值
        item.value = ajaxData[item.key] && ajaxData[item.key] !== 0 ? ajaxData[item.key] : '';
        if (item.key === 'height' && ajaxData[item.key] && ajaxData[item.key] <= 3) {
          item.value = (ajaxData[item.key] * 100).toFixed(2);
        }
        if (item.key === 'BMI' && !ajaxData[item.key] && ajaxData.height && ajaxData.weight) {
          item.value = (
            ajaxData.weight / Math.pow(ajaxData.height <= 3 ? ajaxData.height : ajaxData.height / 100, 2)
          ).toFixed(2);
        }
        if (item.key === 'Scr' && ajaxData.scrUnit === 1) {
          item.unit = 'umol/L';
        }
        if (item.key === 'Scr' && ajaxData.scrUnit === 2) {
          item.unit = 'mg/dl';
        }
      }
      return item;
    });
  });
  return data;
};
